package string.t151;

public class One {
    public String reverseWords(String s) {

        StringBuilder sb = new StringBuilder();

        char[] charArray = s.toCharArray();

        for (int i = charArray.length - 1; i >= 0; i--) {
            if (charArray[i] == (' ')) {
                continue;
            }
            int j = i;
            while (j >= 0 && charArray[j] != (' ')) {
                j--;
            }

            addWord(charArray, i, j + 1, sb);

            sb.append(' ');

            i = j;

        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    private void addWord(char[] charArray, int end, int begin, StringBuilder sb) {
        while (begin <= end) {
            sb.append(charArray[begin]);
            begin++;
        }
    }

}